/**
*JxCard
*parames:
*
*/

function KenDonCard()
{
  /*
   //---------- Attribute -----------
   */
  this.args = arguments;  //  this.args ='sdfsdf';
  this.items = [];         //用于保存产品,JSON格式
  /*
  //--------初始化方法-----------------
  */
  function init(_this){
    if(KenDonCard.instance !== false){
      throw Error('已有一个购物车,不能创建多个购物车 ');
    }else{
      KenDonCard.instance = true;
    }
    //alert(_this.args);
  } //init
  init(this);
  
  /*
   //------------ Method -----------
   */
  /*------------功能方法----------*/
  
  /*创建*/
  this.create = function(){
    var ck = $.cookie(this.config.cardName);
    if(ck){
      this.items = JSON.parse(ck);
      for(var i=0; i<this.items.length; i++){
        hasGoods.push(this.items[i][0]);
      }
    }else{
      //$.cookie(this.config.cardName, {path:'/'});
      this.items = [];
    }
  };  //create
  
  this.createView=function(){
    //创建购物车界面
    this.view = _createView({
      cradName:   this.config.cardName,
      bindNode:   this.config.bindNode,
      cardItemViewTemplate:   this.config.cardItemViewTemplate,
      data:   this.items
    });
    
    var bindNode = this.config.bindNode;
    $(bindNode).append(this.view);
  };
  /*增加产品*/
  this.addItem = new KenDon.Delegate();
  
  /*移除产品*/
  this.removeItem = new KenDon.Delegate();
  
  /*移除所有产品*/
  this.removeAllItem = new KenDon.Delegate();
  
  /*下订单*/
  this.place = new KenDon.Delegate();
  
  /*隐藏界面*/
  this.hide = new KenDon.Delegate();
  
  /*显示界面*/
  this.show = new KenDon.Delegate();

  /*设置*/
  this.setting = function(p){
    try{
      for(member in p){

          if(this[member]){
            //document.write('<div>oprea:'+ member +':<br/>');
            for(var i=0;i<p[member].length;i++){
              //document.write('<textarea  cols="40" rows="10">'+ p[member][i]+'</textarea>');
              this[member].add(p[member][i]);
            } //for
          }else{
            this.config[member] = p[member];
          }
          //document.write('</div>');
      }  //for
    }catch(e){
      throw new Error(e + 'KenDonCard.setting(p):p错误');
    }
    
  };  //setting
  
  /*
  //--------内部类/函数------------
  */

  
  function _createView(obj){
    var tdata = {
      cardName:obj.cradName,
      items:(obj.data.length>0)&&(obj.data)||('')
    };
    //alert(tdata.items);
    //alert(obj.bindNode);
    $('#'+obj.bindNode).setTemplateElement('cardViewTemplate').processTemplate(tdata);
  }  //_createView
}//JxCard

KenDonCard.instance = false;

KenDon.prototype.config = {};
KenDon.prototype.config.cardName = undefined;
KenDon.prototype.config.bindNode = 'body';
KenDon.prototype.config.cardViewTemplate = undefined;
KenDon.prototype.config.cardItemField = undefined;
